package hbase;

/**
 * @author Lu Ruotong
 * @date 2022/3/12 10:11
 */

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HbaseUtil {

    static final String c = "info";


    //     放置初始化方法
    public List<Student> rowQuery(String studentId) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf = HBaseConfiguration.create();// 配置
        conf.set("hbase.zookeeper.quorum", "192.168.116.11");// zookeeper地址
        conf.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
        conf.setInt("hbase.rpc.timeout", 20000);
        conf.setInt("hbase.client.operation.timeout", 20000);
        conf.setInt("hbase.client.scanner.timeout.period", 20000);
        conf.set("hbase.client.ipc.pool.type", "RoundRobinPool");
        HTable table = new HTable(conf, "Student");//改为自己的表名
        List<Student> list = new ArrayList<>();
        Scan scan = new Scan();
        //查询rowkey中的列
        Filter filter1 = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(studentId)));
        scan.setFilter(filter1);
        ResultScanner resultScanner = table.getScanner(scan);


        for (Result result : resultScanner) {
            Student studentTable = new Student();
            String name = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
            String date_ruxue = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("date_ruxue")));
            String telephone = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("telephone")));
            String semester = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("semester")));
            String studentStatus = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("studentStatus")));

            studentTable.setName(name);
            studentTable.setDate_ruxue(date_ruxue);
            studentTable.setTelephone(telephone);
            studentTable.setSemester(semester);
            studentTable.setStudentStatus(studentStatus);
            list.add(studentTable);

        }
        resultScanner.close();
        return list;
    }


    public List<Student> nameQuery(String Name) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf = HBaseConfiguration.create();// 配置
        conf.set("hbase.zookeeper.quorum", "192.168.116.11");// zookeeper地址
        conf.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
        conf.setInt("hbase.rpc.timeout", 20000);
        conf.setInt("hbase.client.operation.timeout", 20000);
        conf.setInt("hbase.client.scanner.timeout.period", 20000);
        conf.set("hbase.client.ipc.pool.type", "RoundRobinPool");
        HTable table = new HTable(conf, "Student");//改为自己的表名
        List<Student> list1 = new ArrayList<>();
        Scan scan = new Scan();
        //查询rowkey中的列
        Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("name"),
                CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(Name)));
        scan.setFilter(filter2);
        ResultScanner resultScanner = table.getScanner(scan);


        for (Result result : resultScanner) {
            Student studentTable = new Student();
            String name = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
            String date_ruxue = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("date_ruxue")));
            String telephone = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("telephone")));
            String semester = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("semester")));
            String studentStatus = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("studentStatus")));

            studentTable.setName(name);
            studentTable.setDate_ruxue(date_ruxue);
            studentTable.setTelephone(telephone);
            studentTable.setSemester(semester);
            studentTable.setStudentStatus(studentStatus);
            list1.add(studentTable);

        }
        resultScanner.close();
        return list1;
    }

    public List<Student> statusQuery(String StudentStatus) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf = HBaseConfiguration.create();// 配置
        conf.set("hbase.zookeeper.quorum", "192.168.116.11");// zookeeper地址
        conf.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
        conf.setInt("hbase.rpc.timeout", 20000);
        conf.setInt("hbase.client.operation.timeout", 20000);
        conf.setInt("hbase.client.scanner.timeout.period", 20000);
        conf.set("hbase.client.ipc.pool.type", "RoundRobinPool");
        HTable table = new HTable(conf, "Student");//改为自己的表名
        List<Student> list = new ArrayList<>();
        Scan scan = new Scan();
        //查询rowkey中的列
        Filter filter3 = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("studentStatus"),
                CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(StudentStatus)));
        scan.setFilter(filter3);
        ResultScanner resultScanner = table.getScanner(scan);


        for (Result result : resultScanner) {
            Student studentTable = new Student();
            String name = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
//            String studentStatus = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("studentStatus")));

            studentTable.setName(name);
//            studentTable.setStudentStatus(studentStatus);
            list.add(studentTable);

        }
        resultScanner.close();
        return list;
    }

    public List<Student> semesterQuery(String semester) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.116.11");// zookeeper地址
        conf.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
        conf.setInt("hbase.rpc.timeout", 20000);
        conf.setInt("hbase.client.operation.timeout", 20000);
        conf.setInt("hbase.client.scanner.timeout.period", 20000);
        conf.set("hbase.client.ipc.pool.type", "RoundRobinPool");


        List<Student> list = new ArrayList<>();

        HTable table = new HTable(conf, "Student");//改为自己的表名

        Scan scan = new Scan();

        Filter filter4 = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("semester"),
                CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(semester)));

        scan.setFilter(filter4);

        ResultScanner resultScanner = table.getScanner(scan);

        for (Result result : resultScanner) {
            Student studentTable = new Student();
            String name = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
            String date = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("date_ruxue")));
            String telephone = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("telephone")));
            String Semester = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("semester")));
            String studentStatus = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("studentStatus")));

            studentTable.setName(name);
            studentTable.setDate_ruxue(date);
            studentTable.setTelephone(telephone);
            studentTable.setSemester(Semester);
            studentTable.setStudentStatus(studentStatus);

            list.add(studentTable);
        }
        resultScanner.close();
        return list;



    }
    public List<Student> dateQuery(String date) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.116.11");// zookeeper地址
        conf.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
        conf.setInt("hbase.rpc.timeout", 20000);
        conf.setInt("hbase.client.operation.timeout", 20000);
        conf.setInt("hbase.client.scanner.timeout.period", 20000);
        conf.set("hbase.client.ipc.pool.type", "RoundRobinPool");


        List<Student> list = new ArrayList<>();

        HTable table = new HTable(conf, "Student");//改为自己的表名

        Scan scan = new Scan();

        Filter filter4 = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("date_ruxue"),
                CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(date)));

        scan.setFilter(filter4);

        ResultScanner resultScanner = table.getScanner(scan);

        for (Result result : resultScanner) {
            Student studentTable=new Student();
            String name= Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("name")));
            String semster= Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("semster")));
            String Date= Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("date_ruxue")));
            String telephone= Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("telephone")));
            String studentStatus= Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("studentStatus")));

            studentTable.setName(name);
            studentTable.setSemester(semster);
            studentTable.setDate_ruxue(Date);
            studentTable.setStudentStatus(telephone);
            studentTable.setStudentStatus(studentStatus);

            list.add(studentTable);
        }
        resultScanner.close();
        return list;


    }

    public String insertinfo(String ip, String port, String tableName, List<Student> list) throws Exception {
        Connection con = getConnection(ip, port);
        HBaseAdmin admin = (HBaseAdmin) con.getAdmin();
        Table table = con.getTable(TableName.valueOf(tableName));
        boolean b = admin.tableExists(TableName.valueOf(tableName));
        if (!b) {
            createTable(admin, tableName);
        }
        insertList(table, list);
        return "添加成功";
    }

    //创建表的方法
    private void createTable(Admin admin, String tableName) throws Exception {
        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
        HColumnDescriptor descriptor2 = new HColumnDescriptor("info");
        descriptor.addFamily(descriptor2);
        admin.createTable(descriptor);
    }

    //插入数据的方法
    private void insertList(Table table, List<Student> list) throws Exception {
        for (Student student : list) {
            Put put = new Put(student.getStudentId().getBytes());
            put.addColumn(c.getBytes(), "name".getBytes(), student.getName().getBytes());
            put.addColumn(c.getBytes(), "date_ruxue".getBytes(), student.getDate_ruxue().getBytes());
            put.addColumn(c.getBytes(), "telephone".getBytes(), student.getTelephone().getBytes());
            put.addColumn(c.getBytes(), "semester".getBytes(), student.getSemester().getBytes());
            put.addColumn(c.getBytes(), "studentStatus".getBytes(), student.getStudentStatus().getBytes());
            table.put(put);
        }
    }


    //获得与hbase的连接
    private static Connection getConnection(String ip, String port) throws Exception {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", ip);
        configuration.set("hbase.zookeeper.property.clientPort", port);
        Connection connection = ConnectionFactory.createConnection(configuration);
        return connection;
    }


    public void main(String[] args) {
        ArrayList<Student> studentArrayList = new ArrayList<>();
        Student student = new Student();
//        student.setStudentId("2020001");
//        student.setName("Tom");
//        student.setDate_ruxue("20200801");
//        student.setTelephone("1512326111");
//        student.setSemester("1");
//        student.setStudentStatus("L");

        studentArrayList.add(student);
        try {
            insertinfo("niit01", "2181", "Student", studentArrayList);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
